#!/usr/bin/perl
#
use Ogame;
use Ogame::Shell;
use Getopt::Long;
use strict;

$|=1;

&__main;

sub __main() {
my $_o_action;
my $_o_section;
my $_o_help;
my $_o_conffile;
my $_o_debug;
my ($_o_origin, $_o_destination,$_o_metal,$_o_cristal,$_o_deuterium,$_o_planetid,$_o_batiment);
my ($_o_galaxie, $_o_systeme, $_o_nscan,$_o_type_p,$_o_number);
my ($_o_type_f, $_o_qty, $_o_pt,$_o_gt,$_o_cle,$_o_clo,$_o_croi,$_o_vb,$_o_sonde,$_o_EDM,$_o_bomb,$_o_destr);
my ($_o_colo,$_o_traqueur,$_o_sat,$_o_recycl,$_o_bf);
my ($_o_type_d,$_o_lm,$_o_lle,$_o_llo,$_o_cg,$_o_ai);
my ($_o_lp,$_o_pb,$_o_gb,$_o_amip,$_o_mip);
my ($_o_fichier,$_o_max,$_o_delai, $_o_logout,$_o_type_t,$_o_techno);
my ($_o_dump_txt,$_o_dump_HTML,$_o_page);
my $result = GetOptions("action|A=s" 		=> \$_o_action,
								"section|S=s" 		=> \$_o_section,
								"conf|F=s" 			=> \$_o_conffile,
								"help|h|?" 			=> \$_o_help,
								"debug"				=> \$_o_debug,
								"origin|o=s" 		=> \$_o_origin,
								"destination|d=s" => \$_o_destination,
								"metal=n" 			=> \$_o_metal,
								"cristal=n" 		=> \$_o_cristal,
								"deuterium=n" 		=> \$_o_deuterium,
								"planetid|p=n" 	=> \$_o_planetid, #peut-etre egal a origin, a voir plus tard
								"batiment|b=s" 	=> \$_o_batiment,
								"galaxie=n" 		=> \$_o_galaxie,
								"systeme=n" 		=> \$_o_systeme,
								"type_p|t=s" 		=> \$_o_type_p,
								"number|n=n" 		=> \$_o_number,
								"nscan=n" 			=> \$_o_nscan,
								"build_fleet|bf=n"=> \$_o_bf,
								"type_f=s" 			=> \$_o_type_f,
								"type_d=s"			=> \$_o_type_d,
								"type_t=s"			=> \$_o_type_t,
								"qty=n" 				=> \$_o_qty,
								"pt=n" 				=> \$_o_pt,
								"gt=n" 				=> \$_o_gt,
								"cle=n" 				=> \$_o_cle,
								"clo=n" 				=> \$_o_clo,
								"croi=n" 			=> \$_o_croi,
								"recycl=n" 			=> \$_o_recycl,
								"vb=n" 				=> \$_o_vb,
								"sat=n" 				=> \$_o_sat,
								"sonde=n" 			=> \$_o_sonde,
								"bomb=n" 			=> \$_o_bomb,
								"destr=n" 			=> \$_o_destr,
								"EDM=n" 				=> \$_o_EDM,
								"traqueur=n"		=> \$_o_traqueur,
								"fichier=s"			=> \$_o_fichier,
								"max=n"				=> \$_o_max,
								"delai=n"			=> \$_o_delai,
								"logout"				=> \$_o_logout,
								"build_techno"		=> \$_o_techno,
								"dump_txt"		=> \$_o_dump_txt,
								"dump_HTML"		=> \$_o_dump_HTML,
								"page=s"			=> \$_o_page,
								);

if ($_o_help || (!$result || !$_o_section)) {
	&usage();
}

if (! $_o_action) {
	&printerr("no action defined, exiting.");
	exit -1;
}

my %params;
$params{section} = $_o_section;
$params{config} = $_o_conffile;
if ($_o_debug) {$params{debug}=1;}
if ($_o_dump_txt) {$params{dump_txt}=1;}
if ($_o_dump_HTML) {$params{dump_HTML}=1;}


my $empire = Ogame->new(%params);
$empire->read_config() or die "Impossible to read config";
$empire->get_lock();
$empire->ogame_init(); # Login & get planet list

if ( ($_o_action =~ "transport") && ($#ARGV == 7) ){
		my $trans_src=shift;		
		my $all_ships=shift;
		my $trans_dest=shift;		
		my $trans_mode=shift;		#1 -> attaquer #3 -> transporter #4 -> stationner #8 -> recycler
		my $trans_met=shift;		
		my $trans_cri=shift;		
		my $trans_deut=shift;		
		my $trans_speed=shift;		
		&printlog($trans_src," ",$all_ships," ",$trans_dest," ",$trans_mode," ",$trans_met," ",$trans_cri," ",$trans_deut," ",$trans_speed);
		$empire->transport($trans_src,$all_ships,$trans_dest,$trans_mode,$trans_met,$trans_cri,$trans_deut,$trans_speed);
}
elsif ($_o_action eq "recycle"){
	$empire->recycle($_o_origin,$_o_destination,$_o_recycl);
}
elsif ($_o_action eq "expedie"){
	my @fleet=($_o_pt,$_o_gt,$_o_cle,$_o_clo,$_o_croi,$_o_vb,$_o_colo,$_o_recycl,$_o_sat,$_o_bomb,$_o_sat,$_o_destr,$_o_EDM,$_o_traqueur);
	for (my $i=0;$i<14;$i++){
		if (!defined($fleet[$i])){
			$fleet[$i]=0;
	  	}
	}
	if ($_o_destination =~ /\d+:\d+:16/) {
		$empire->expedie($_o_origin,$_o_destination,@fleet);
	}
	else {
		&printerr("Expedie: $_o_destination destination non valide");
	}
}
elsif ($_o_action eq "attaque"){
	my @fleet=($_o_pt,$_o_gt,$_o_cle,$_o_clo,$_o_croi,$_o_vb,$_o_colo,$_o_recycl,$_o_sat,$_o_bomb,$_o_sat,$_o_destr,$_o_EDM,$_o_traqueur);
	for (my $i=0;$i<14;$i++){
		if (!defined($fleet[$i])){
			$fleet[$i]=0;
	  	}
	}
	if ($_o_destination =~ /\d+:\d+:\d+/) {
		$empire->attaque($_o_origin,$_o_destination,@fleet);
	}
	else {
		&printerr("Attaque: $_o_destination destination non valide");
	}
}
elsif ($_o_action eq "multi_sonde"){ ##
	if (($_o_origin>=0) && $_o_fichier && $_o_sonde && $_o_max && $_o_delai) {
		$empire->multi_sonde2($_o_origin,$_o_fichier,$_o_sonde,$_o_max,$_o_delai);#
	}
	else {
		&printerr("Action: multi_sonde requested but missing arg");
	}
}

elsif ($_o_action eq "sonde"){ ## Test OK
	if (($_o_origin>=0) && $_o_destination && $_o_type_p && $_o_sonde) {
		$empire->sonde($_o_origin,$_o_destination,$_o_type_p,$_o_sonde);#
	}
	else {
		&printerr("Action: sonde requested but missing arg");
	}
}
elsif ($_o_action eq "check"){ ## Test OK
	$empire->alerte();
}
elsif ($_o_action eq "display") { ## Test OK
	$empire->scan_empire();
	$empire->display_resources();
	$empire->display_fleet();
	$empire->display_buildings();
}
elsif ($_o_action eq 'scan') { ## Test OK
	if ($_o_galaxie && $_o_systeme && $_o_nscan && defined($_o_planetid)) {
		$empire->scan($_o_galaxie,$_o_systeme,$_o_nscan,$_o_planetid);
	}
	else {
		&printerr("Action: scan requested but missing arg");
	}
}
elsif ($_o_action eq 'scan_and_spy') { ## Test OK
	if ($_o_galaxie && $_o_systeme && $_o_nscan && defined($_o_planetid)) {
		$empire->scan_and_spy(galaxie => $_o_galaxie, systeme => $_o_systeme, nbsysteme => $_o_nscan, planetid => $_o_planetid);
	}
	else {
		&printerr("Action: scan requested but missing arg");
	}
}
elsif ($_o_action eq "build") {
	$empire->build($_o_planetid,$_o_batiment);
}
elsif ($_o_action eq "build_def") {
        print "planetid: $_o_planetid type: $_o_type_d qty: $_o_qty\n";
        if (defined($_o_planetid) && $_o_type_d && $_o_qty) {
                $empire->build_def($_o_planetid, $_o_type_d,$_o_qty);
        }
}
elsif ($_o_action eq "bf"){ ## Test OK
	print "planetid: $_o_planetid type: $_o_type_f qty: $_o_qty\n";    
	if (defined($_o_planetid) && $_o_type_f && $_o_qty) {
		$empire->build_flotte($_o_planetid,$_o_type_f,$_o_qty);#
	}
	else {
		&printerr("Action: build fleet requested but args missing");
	}
	#build_flotte($_o_origin,$_o_type_f,$_o_qty);#    
}
elsif ($_o_action eq "escape") {
	$empire->escape($_o_planetid);
}
elsif ($_o_action eq "testnearest") {
	$empire->testnearest();
}
elsif ($_o_action eq "read_messages"){
	$empire->read_messages();
}
elsif ($_o_action eq "del_messages"){
	$empire->del_messages();
}
elsif ($_o_action eq "del_all_messages"){
	$empire->del_all_messages();
}
elsif ($_o_action eq "get_messages"){
	while ($empire->get_messages()) {
		$empire->del_messages();
	}
}
elsif ($_o_action eq "get_messages2"){
	while ($empire->get_messages2()) {
		$empire->del_messages();
	}
}
elsif ($_o_action eq "_get_messages"){
	$empire->get_messages();
}
elsif ($_o_action eq "_get_messages2"){
	$empire->get_messages2();
}
elsif ($_o_action eq 'colonise') {
	$empire->send_fleet(PlanetID => $_o_planetid,
								Vaisseaux => { 'colo' => 1 },
								DestCoord => $_o_destination,
								DestType => 'Planete',
								Speed => '100',
								Mission => 'Coloniser',
								Metal => 0,
								Cristal => 0,
								Deuterium => 0); }
elsif ($_o_action eq 'stationne') {
	my %vaisseaux;
	if($_o_pt) { $vaisseaux{'pt'}=$_o_pt; }
	if($_o_gt) { $vaisseaux{'gt'}=$_o_gt; }
	if($_o_cle) { $vaisseaux{'cle'}=$_o_cle; }
	if($_o_clo) { $vaisseaux{'clo'}=$_o_clo; }
	if($_o_croi) { $vaisseaux{'croi'}=$_o_croi; }
	if($_o_recycl) { $vaisseaux{'recycl'}=$_o_recycl; }
	if($_o_vb) { $vaisseaux{'vb'}=$_o_vb; }
	if($_o_sonde) { $vaisseaux{'sonde'}=$_o_sonde; }
	if($_o_bomb) { $vaisseaux{'bomb'}=$_o_bomb; }
	if($_o_destr) { $vaisseaux{'destr'}=$_o_destr; }
	if($_o_EDM) { $vaisseaux{'EDM'}=$_o_EDM; }
	if($_o_traqueur) { $vaisseaux{'traqueur'}=$_o_traqueur; }
	$empire->send_fleet(PlanetID => $_o_planetid,
								Vaisseaux => \%vaisseaux,
								DestCoord => $_o_destination,
								DestType => $_o_type_p,
								Speed => '100',
								Mission => 'Stationner',
								Metal => 0,
								Cristal => 0,
								Deuterium => 0); 
}

elsif ($_o_action eq 'changeplanet') {
	if ($_o_planetid) {
		$empire->change_planet($_o_planetid);
	}
}
elsif ($_o_action eq 'get_flying') {
	$empire->get_flying();
}
elsif ($_o_action eq 'logout') {
	$empire->logout();
}
elsif ($_o_action eq "build_techno") {
	$empire->build_techno($_o_planetid,$_o_type_t);
}
elsif ($_o_action eq "show_page") {
	$empire->show_page($_o_planetid,$_o_page);
}
elsif ($_o_action eq "shell") {
	my $shell=Ogame::Shell->new();
	$shell->init($empire,$_o_section);
	$shell->cmdloop;
}
else {
	&printerr("unknown action $_o_action");
}

if($_o_logout) {
	$empire->logout();
}
$empire->close_db();
$empire->release_lock();
} # /END __main()


sub usage() {
	die 
"Usage: ogbot.pl	-a, --action=[ check |
			attack |
			transport |
			build	{-p planet_id -b batiment_type} |
			sonde	{-o planet_id -d dest_coord -t type_dest -n nombre_sondes} |
			scan	{--planetid planet_id --nscan nb_scan --galaxie G --systeme S} |
			bf	{-p planet_id -type_f type_vaisseau -qty quantite} ]
		-s, --section=SECTION	Use SECTION section in ~/.ogamerc
		-f, --conf=<config file>

exemples:
	./ogbot.pl -s 25 -a check
	./ogbot.pl -s 25 -a display
	./ogbot.pl -s 25 -a logout
	./ogbot.pl -s 25 -a scan --galaxie=2 --systeme=100 --nscan=30 --planetid=34215671
	./ogbot.pl -s 25 -a bf -p 0 -type_f vb -qty 1
	./ogbot.pl -s 25 -a sonde -0 0 -d 4:367:10 -t Planete -sonde 1
	./ogbot.pl -s 25 -a multi_sonde -fichier s25.txt -sonde 1 -o 12890178 -max 11 -delai 180
	./ogbot.pl -s 25 -a get_messages
	./ogbot.pl -s 25 -a del_messages
	./ogbot.pl -s 25 -a del_all_messages
	./ogbot.pl -s 25 -a attaque -gt 2 -d 7:11:11 -o 32890178
	./ogbot.pl -s 25 -a recycle -recycl 2 -d 7:11:11 -o 32890178
	./ogbot.pl -s 25 -a build -b electrique -p 3
	./ogbot.pl -s 25 -a stationne -pt 1 -traqueur 10 -d 7:11:11 -t Lune -o 32890178
	./ogbot.pl -s 25 -a expedie -pt 10 -d 7:11:16 -o 6
	./ogbot.pl -s 25 -a build_def -p 0 -type_d lm -qty 10
	./ogbot.pl -s 25 -a build_techno -p 0 -type armes 
";
}

